// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const tcbRouter = require('tcb-router')
const rp = require('request-promise')

const BASE_URL = 'https://api.weixin.qq.com'

// 云函数入口函数
exports.main = async (event, context) => {
  const app = new tcbRouter({
    event
  })

  const wxContext = cloud.getWXContext()
  const result = await cloud.openapi.wxacode.getUnlimited({
    scene: wxContext.OPENID,
    // page: "pages/blog/blog"
    // lineColor: {
    //   'r': 211,
    //   'g': 60,
    //   'b': 57
    // },
    // isHyaline: true
  })
  // console.log(result)
  // 上传文件
  const upload = await cloud.uploadFile({
    cloudPath: 'qrcode/' + Date.now() + '-' + Math.random() + '.png',
    fileContent: result.buffer
  })
  return upload.fileID



  // app.router('getToken', async (ctx, next) => {
  //   ctx.body = await rp(BASE_URL + '/cgi-bin/token?grant_type=client_credential&appid=wx50533ebde3cfca66&secret=9999d5e881e64e8713bc0af6dcb97e37')
  //     .then((res) => {
  //       return res
  //     })
  // })
}